/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core.defaultActions;

import java.awt.Point;
import java.util.ArrayList;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;

public class BasicMove implements Action {

	public BasicMove() {

	}

	@Override
	public Parameters call(Parameters parameters) {
		String subaction = (String) parameters.get("subAction");
		Element me = (Element) parameters.get("me");
		boolean xcollision = false;
		boolean ycollision = false;

		Point stdspeed = new Point(me.getSpeed());
		Point dotspeed = new Point((Point) me.getProperty(".speed"));

		Point oldspeed = new Point();

		oldspeed.x = dotspeed.x + stdspeed.x;
		oldspeed.y = dotspeed.y + stdspeed.y;

		Point speed = new Point(oldspeed);

		// Common.info("speed :"+dotspeed);
		Point position = new Point(me.getPosition());
		boolean collision = false;
		boolean canStop = (Boolean) me.getProperty("canStop");
		if (!me.xcanCollide(speed.x) && speed.x != 0) {

			position.x += speed.x;
		} else if (speed.x != 0) {

			int verso = Math.abs(speed.x) / speed.x;
			for (int i = 0; i < Math.abs(speed.x); i++) {

				if (!me.xcanCollide(verso * (i + 1)) && speed.x != 0) {
					position.x += verso;
				}
			}
			collision = true;
			xcollision = true;
			if (canStop)
				speed.x = 0;

		}

		if (!me.ycanCollide(speed.y) && speed.y != 0)
			position.y += speed.y;
		else if (speed.y != 0) {
			int verso = Math.abs(speed.y) / speed.y;
			for (int i = 0; i < Math.abs(speed.y); i++) {
				if (!me.ycanCollide(verso * (i + 1)) && speed.y != 0)
					position.y += verso;
			}
			collision = true;
			if (canStop)
				speed.y = 0;

			ycollision = true;
			// actions.runAction("move", new
			// Parameters(stdinfo).put("subAction", "stop"));
		}

		Point tmpSpeed = new Point(speed);
		// if(speed.x!=0) tmpSpeed.x += Math.abs(speed.x)/speed.x;
		// if(speed.y!=0) tmpSpeed.y += Math.abs(speed.y)/speed.y;
		if (me.canCollide(tmpSpeed)) {
			if (canStop)
				speed.x = speed.y = 0;
			position = me.getPosition();
			collision = xcollision = ycollision = true;
		}

		ArrayList<Element> collisions = ((ArrayList<Element>) (me
				.getProperty("collisionList")));
		if (collisions == null)
			collisions = new ArrayList<Element>();

		if (collision) {
			if (canStop)
			speed.x = speed.y = 0;
			// Point oldspeed = me.getSpeed();
			collisions = me.level.getCollisions(me, me
					.getTraceRectangle(oldspeed));
			// alert others of the collsiion! brrr
			for (Element e : collisions) {
				ArrayList<Element> al = (ArrayList<Element>) e
						.getProperty("collisionList");
				if (al == null)
					al = new ArrayList<Element>();
				al.add(me);
				e.setProperty("collisionList", al);
			}

		//	Common.info("BsaicMove: collision from " + me + " to" + collisions);
		}
		// me.setProperty("xcollision",collision);
		// me.setProperty("ycollision",true);
		me.setProperty("isColliding", collision);
		me.setProperty("isXColliding", collision);
		me.setProperty("isYColliding", collision);
		me.setProperty("collisionList", collisions);
		me.setSpeed(speed);
		me.setPosition(position);
		me.setProperty(".speed", new Point(0, 0));
		return parameters;

	}

}
